/* 
  * File:   cAbstractGame.h
 * Author: josep
 *
 * Created on 12 / gener / 2011, 02:31
 */

#ifndef CABSTRACTGAME_H
#define	CABSTRACTGAME_H

#include "cData.h"
#include "cCamera.h"
#include "cSceneManager.h"
#include "utility.h"

class cAbstractGame {
public:
    cAbstractGame(){
		for(int i=0; i<256; i++){
			keys[i]=0;
		}
                timeUnit=TIME_UNIT;
                minElapsedTime=MIN_ELAPSED_TIME;
	}
    //cAbstractGame(const cAbstractGame& orig){}
    virtual ~cAbstractGame(){}
    virtual bool Init(cData *Data, cCamera* Cam)=0;
    virtual bool Init(const char* pName, cData *Data, cCamera* Cam){
        string buffer;
        string iniFileName;
        if(strstr(pName, ".ini")==NULL){
            iniFileName = concatToString("ss", pName, ".ini"); // << pName << ".ini";
        }else{
            iniFileName = pName; //<< pName;
        }
        ifstream inFileStream(iniFileName.c_str());
        if(!inFileStream.is_open()) {
              throw PECVJ2D_ERROR_FILE_NOT_FOUND;
        }
        while(getline(inFileStream, buffer)) {
            if(buffer[0]=='M' && buffer[1]=='I'){
                minElapsedTime=stringToint(buffer.substr(buffer.find(CHAR_EQUAL)+1));
            }else if(buffer[0]=='T' && buffer[1]=='I'){
                timeUnit=stringToint(buffer.substr(buffer.find(CHAR_EQUAL)+1));
            }
        }
        inFileStream.close();
        this->Init(Data, Cam);
    }
    virtual int Loop()=0;
    virtual void Finalize()=0;

    //Input
    virtual void ReadKeyboard(unsigned char key, int x, int y, bool press)=0;
    virtual void ReadMouse(int button, int state, int x, int y)=0;
    //Process
    virtual void Process()=0;
    //Output
    virtual void Render()=0;
    cCamera* GetCamera(){return Camera;}
    void SetEstate(int estate) {
        this->gameState = estate;
        nextGame=estate;
    }
    int GetEstate() const {
        return gameState;
    }
protected:
    cData *Data;
    int nextGame;
    cCamera* Camera;
    unsigned char keys[256];
    int widthOfWorld;  //in tiles
    int heightOfWorld; //in tiles
    cSceneManager SceneManager;
    int minElapsedTime;
    int timeUnit;
    int gameState;

private:

};

#endif	/* CABSTRACTGAME_H */

